package com.ruoyi.web.controller.system;

import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.excel.EasyExcel;
import com.ruoyi.system.domain.Diagnose;
import com.ruoyi.system.excel.ExcelUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Surgery;
import com.ruoyi.system.service.ISurgeryService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 手术Controller
 * 
 * @author ruoyi
 * @date 2024-08-21
 */
@RestController
@RequestMapping("/system/surgery")
public class SurgeryController extends BaseController
{
    @Autowired
    private ISurgeryService surgeryService;

    /**
     * 查询手术列表
     */
    //@PreAuthorize("@ss.hasPermi('system:surgery:list')")
    @GetMapping("/list")
    public TableDataInfo list(Surgery surgery)
    {
        startPage();
        List<Surgery> list = surgeryService.selectSurgeryList(surgery);
        return getDataTable(list);
    }

    /**
     * 导出手术列表
     */
    @PreAuthorize("@ss.hasPermi('system:surgery:export')")
    @Log(title = "手术", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Surgery surgery)
    {
        List<Surgery> list = surgeryService.selectSurgeryList(surgery);
        ExcelUtil<Surgery> util = new ExcelUtil<Surgery>(Surgery.class);
        util.exportExcel(response, list, "病案信息手术管理");
    }

    /**
     * 获取手术详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:surgery:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(surgeryService.selectSurgeryById(id));
    }

    /**
     * 新增手术
     */
    @PreAuthorize("@ss.hasPermi('system:surgery:add')")
    @Log(title = "手术", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Surgery surgery)
    {
        return toAjax(surgeryService.insertSurgery(surgery));
    }

    /**
     * 修改手术
     */
    @PreAuthorize("@ss.hasPermi('system:surgery:edit')")
    @Log(title = "手术", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Surgery surgery)
    {
        return toAjax(surgeryService.updateSurgery(surgery));
    }

    /**
     * 删除手术
     */
    @PreAuthorize("@ss.hasPermi('system:surgery:remove')")
    @Log(title = "手术", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(surgeryService.deleteSurgeryByIds(ids));
    }

    @PostMapping("/importTemplate")
    @PreAuthorize("@ss.hasPermi('system:surgery:importTemplate')")
    public void importTemplate(HttpServletResponse response) throws IOException {
        //模版路径
        InputStream resourceAsStream = ExcelUtils.class.getClassLoader().getResourceAsStream("excel-template/surgery.xlsx");

        // 设置 header 和 contentType。写在最后的原因是，避免报错时，响应 contentType 已经被修改了
        response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("病案信息手术管理导入模版.xlsx", "UTF-8"));
        response.setContentType("application/vnd.ms-excel;charset=UTF-8");

        EasyExcel.write(response.getOutputStream()).withTemplate(resourceAsStream).sheet().doFill(CollUtil.newHashMap());
    }

    @Log(title = "手术导入", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('system:surgery:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<Surgery> excelUtil = new ExcelUtil<Surgery>(Surgery.class);
        List<Surgery> surgeryList = excelUtil.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = surgeryService.importCase(surgeryList, updateSupport, operName);
        return success(message);
    }

}
